﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace RGB.SharePoint.Util.EmailUtility
{
	public class EmailConfigurationManager : IEmailConfigurationManager
	{
		private SPWeb web;

		public EmailConfigurationManager(SPWeb web)
		{
			this.web = web;
		}

		/// <summary>
		/// Get Email Template by it's ID
		/// </summary>
		/// <param name="emailTemplateId"></param>
		/// <returns></returns>
		public EmailTemplate GetEmailTemplate(string emailTemplateId)
		{
			EmailTemplate emailTemplate = null;
			if (!string.IsNullOrEmpty(emailTemplateId))
			{
				SPList emailConfigurationList = web.Lists.TryGetList("Email Template Configuration List");
				if (emailConfigurationList != null)
				{
					SPQuery query = new SPQuery();
					query.Query = string.Format(@"<Where><Eq>
                                 <FieldRef Name='EmailTemplateId'/>
                                 <Value Type='Text'>{0}</Value>
                              </Eq></Where>", emailTemplateId);
					SPListItemCollection items = emailConfigurationList.GetItems(query);
					if (items != null && items.Count > 0)
					{
						SPListItem emailConfigItem = items[0];
						if (emailConfigItem != null)
						{
							emailTemplate = new EmailTemplate();
							emailTemplate.EmailTemplateId = emailTemplateId;
							if (emailConfigItem["ShortDescription"] != null)
							{
								emailTemplate.ShortDescription = emailConfigItem["ShortDescription"].ToString();
							}
							if (emailConfigItem["EmailTemplateSubject"] != null)
							{
								emailTemplate.EmailTemplateSubject = emailConfigItem["EmailTemplateSubject"].ToString();
							}
							if (emailConfigItem["EmailTemplateBody"] != null)
							{
								emailTemplate.EmailTemplateBody = emailConfigItem["EmailTemplateBody"].ToString();
							}
						}
					}
				}
			}
			return emailTemplate;
		}

		/// <summary>
		/// Gets Email Template by it's ID by updating the email template properties
		/// </summary>
		/// <param name="emailTemplateId"></param>
		/// <param name="subjectProperties"></param>
		/// <param name="bodyProperties"></param>
		/// <returns></returns>
		public EmailTemplate GetEmailTemplate(string emailTemplateId, IDictionary<string, string> subjectProperties, IDictionary<string, string> bodyProperties)
		{
			EmailTemplate emailTemplate = null;
			emailTemplate = GetEmailTemplate(emailTemplateId);
			emailTemplate.EmailTemplateSubject = GetUpdatedValue(emailTemplate.EmailTemplateSubject, subjectProperties);
			emailTemplate.EmailTemplateBody = GetUpdatedValue(emailTemplate.EmailTemplateBody, bodyProperties);
			return emailTemplate;
		}


		/// <summary>
		/// Add new email template.
		/// </summary>
		/// <param name="emailTemplateId"></param>
		/// <param name="emailTemplateSubject"></param>
		/// <param name="emailTemplateBody"></param>
		public void AddEmailTemplate(string emailTemplateId, string shortDescription, string emailTemplateSubject, string emailTemplateBody)
		{
			if (!string.IsNullOrEmpty(emailTemplateId) && !string.IsNullOrEmpty(emailTemplateSubject))
			{
				SPSite site = SPContext.Current.Site;
				SPWeb web = site.RootWeb;
				SPList emailConfigurationList = web.Lists.TryGetList("Email Template Configuration List");
				if (emailConfigurationList != null)
				{
					SPListItem emailConfigItem = emailConfigurationList.Items.Add();
					emailConfigItem["EmailTemplateId"] = emailTemplateId;
					emailConfigItem["ShortDescription"] = shortDescription;
					emailConfigItem["EmailTemplateSubject"] = emailTemplateSubject;
					emailConfigItem["EmailTemplateBody"] = emailTemplateBody;
					emailConfigItem.Update();
				}
			}
		}

		private string GetUpdatedValue(string message, IDictionary<string, string> properties)
		{
			string updatedMessage = message;
			if (!string.IsNullOrEmpty(updatedMessage) && properties != null)
			{
				foreach (KeyValuePair<string, string> property in properties)
				{
					updatedMessage = updatedMessage.Replace("&lt;%" + property.Key + "%&gt;", property.Value);
					updatedMessage = updatedMessage.Replace("<%" + property.Key + "%>", property.Value);
				}
			}
			return updatedMessage;
		}
	}
}
